﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WFNetLib;
using Common;
using DAL;
using System.Web.Security;

namespace 服务器端.webAPI
{
    public partial class UserReg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string post = WFHttpWebResponse.PostInput(Request.InputStream);
            RegResult ret = new RegResult();
            try
            {
                UserInfoData user = WFJSON.JsonDeserializeBySingleData<UserInfoData>(post);
                if (UserInfoDataDBOption.Insert(user) == 0)
                {
                    ret.message = "err";
                }
                else
                {
                    MembershipUser User;
                    try
                    {
                        User = Membership.CreateUser(user.ID, user.PhoneNum, user.PhoneNum+"@163.com");
                        Roles.AddUserToRole(user.ID, "会员");
                        ret.message = "ok";
                    }
                    catch (MembershipCreateUserException ex)
                    {
                        UserInfoDataDBOption.Delete(user);
                        ret.message = "err:"+ex.Message;
                    }                    
                }
            }
            catch (System.Exception ex)
            {
                ret.message = "未知错误" + ex.Message;
            }
            ret.DateTime = DateTime.Now.ToString();
            Response.Write(WFJSON.JsonSerializerBySingleData<RegResult>(ret));
        }
    }
}